﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using BUL;
using Public;

namespace Presentation
{
    public partial class frmManageBrand : Form
    {
        BrandBUL brand = new BrandBUL();
        int selectedBrandId;
        public frmManageBrand()
        {
            InitializeComponent();
        }

        private void frmManageBrand_Load(object sender, EventArgs e)
        {
            searchProName("");
        }
        private void btnSearch_Click(object sender, EventArgs e)
        {
            string searchValue = txtSearch.Text.Trim();
            searchProName(searchValue);
        }

        public void searchProName(string searchValue)
        {
            dgvBrand.DataSource = brand.searchBrand(searchValue);
        }

        private void btnAdd_Click(object sender, EventArgs e)
        {
            Brand br = new Brand();
            br.BrandName = txtBName.Text.Trim();
            brand.AddBrand(br);
            frmManageBrand_Load(sender, e);
        }

        private void btnUpdate_Click(object sender, EventArgs e)
        {
            Brand br = new Brand();
            br.BrandName = txtBName.Text.Trim();
            br.BrandId = this.selectedBrandId;
            brand.UpdateBrand(br);
            frmManageBrand_Load(sender, e);
        }

        private void selectedItem(object sender, DataGridViewCellEventArgs e)
        {
            int index = dgvBrand.CurrentRow.Index;
            string brandId = dgvBrand.Rows[index].Cells[0].Value.ToString();
            this.selectedBrandId = int.Parse(brandId);
            this.txtBName.Text = dgvBrand.Rows[index].Cells[1].Value.ToString();
        }

        private void frmManageBrand_FormClosing(object sender, FormClosingEventArgs e)
        {
            DialogResult rs = MessageBox.Show("Are you really want to exist?", "Confirm", MessageBoxButtons.OKCancel, MessageBoxIcon.Question);
            if (rs == DialogResult.OK)
            {
                frmManageBatch manageBatch = new frmManageBatch();
                this.Hide();
                manageBatch.Show();
            }
            else
            {
                e.Cancel = true;
            }
        }
    }
}
